package com.youshang520i.aspect;

import com.youshang520i.service.HelloService;
import com.youshang520i.service.impl.HelloServiceImpl;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import java.lang.reflect.Method;

/**
 * 使用proxyFactory
 * @author youshang
 */
@Component
public class MySpringProxyModel {


    @PostConstruct
    public  void init() {
        Class[] interfacess = new Class[]{HelloService.class};
        //将需要代理的接口添加进proxyFactory
        ProxyFactory proxyFactory = new ProxyFactory(interfacess);
        //选择代理类
        proxyFactory.setTarget(new HelloServiceImpl());

        //待测试
        proxyFactory.setOpaque(true);

//        proxyFactory.setProxyTargetClass(true);

        proxyFactory.addAdvice(new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                System.out.println("1---在方法调用之前拦截");
            }
        });



//        proxyFactory.addAdvisor(new M);

        proxyFactory.addAdvice(new AfterReturningAdvice() {
            @Override
            public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
                System.out.println("方法完成之后调用的方法---1");
            }
        });

        proxyFactory.addAdvice(new MethodInterceptor() {
            @Nullable
            @Override
            public Object invoke(@Nonnull MethodInvocation invocation) throws Throwable {

                System.out.println("1---环绕通知生成的结果：");
                return invocation.proceed();
            }
        });



        Object proxy = proxyFactory.getProxy(proxyFactory.getClass().getClassLoader());
        Class<?>[] interfaces = proxy.getClass().getInterfaces();
        for (Class<?> anInterface : interfaces) {


            //proxyFactory.setProxyTargetClass(true);
            //com.youshang520i.service.HelloService
            //org.springframework.aop.SpringProxy
            //org.springframework.aop.framework.Advised
            //org.springframework.cglib.proxy.Factory

            //proxyFactory.setOpaque(true);
            //com.youshang520i.service.HelloService
            //org.springframework.aop.SpringProxy
            //org.springframework.core.DecoratingProxy


            System.out.println(anInterface.getName());
        }

        HelloService proxy1 = (HelloService) proxy;
        System.out.println(proxy1.hello());

    }



}
